﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WF.Common;

namespace WF.Web.Components.WorkSpace
{
    public partial class OutOfOfficeList : BaseUserControl
    {
        protected string viewOpNo = "002.007.OP01";
        private string viewAllOpNo = "002.007.OP03";
        private string searchFlag = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            this.searchFlag = this.Request.QueryString["SearchFlag"];
            //if (this.searchFlag == "1")
            //{
            //    if (!this.CheckPersonalRight(viewOpNo, false, false))
            //    {
            //        this.ShowMessage("无此操作权限！", true);
            //        this.WindowHistoryGo(-1);
            //        return;
            //    }
            //}
            //else
            //{
            //    if (!this.CheckPersonalRight(viewAllOpNo, false, false))
            //    {
            //        this.ShowMessage("无此操作权限！", true);
            //        this.WindowHistoryGo(-1);
            //        return;
            //    }
            //}

            ParamInit();
            this.dgList.DataManager = this.DataManager;

            if (this.Request["hidIsRebind"] == "true" || this.searchFlag == "1")
            {
                this.LoadData();
            }
        }

        protected void ParamInit()
        {
            if (this.searchFlag == "1")
            {
                this.trToolBar.Visible = false;
                this.trSearch.Visible = false;
                if (!string.IsNullOrEmpty(this.LogonUser.EmpID.Trim()))
                {
                    string sql = string.Format(@" IF Not Exists (Select EmpID from WF_OutOfOfficeSet Where EmpID='{0}') 
                                                Insert Into WF_OutOfOfficeSet(ItemID,EmpID) Values(NewID(),'{0}')", this.LogonUser.EmpID);
                    this.DataManager.Execute(sql);
                }
            }
        }

        protected void LoadData()
        {
            string sql = "Select * from VW_WF_OutOfOfficeList  Where 1=1 ";
            if (this.searchFlag != "1")
            {
                if (this.txtUser.Text.Trim() != "")
                {
                    sql += string.Format(@" And (EmpName Like '{0}%' or ADUserID Like '{0}%')", txtUser.Text.Trim());
                }

                if (this.txtForward.Text.Trim() != "")
                {
                    sql += string.Format(@" And (ForwardEmpName Like '{0}%' or ForwardADUserID Like '{0}%')", txtForward.Text.Trim());
                }

                if (rblState.SelectedValue != "")
                {
                    sql += string.Format(@" And Type = '{0}'", rblState.SelectedValue);
                }
            }
            else
            {
                sql += string.Format(@" And EmpID = '{0}'", this.LogonUser.EmpID);
            }

            sql += " Order By EmpName ";
            this.dgList.DataSourceSQL = sql;
            this.dgList.BindData();
            DataGridUtils.SpanGrid(this.dgList, 0, 5);
        }

        protected void dgList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex >= 0)
            {
                DataGridUtils.SpanGrid(this.dgList, 1, 5);
                string url = "OutOfOfficeEdit.aspx?CmdType=MOD&ItemID=" + e.Item.Cells[0].Text + "&" + DateTime.Now.ToString();
                e.Item.Cells[1].Attributes.Add("onMouseOver", "this.className='tdActionOver';");
                e.Item.Cells[1].Attributes.Add("onMouseOut", "this.className='tdActionOut';");
                e.Item.Cells[1].Attributes.Add("onclick", "OpenAction('" + url + "','dialogWidth:500px;dialogHeight:400px;status:Modal;help:no'); return false;");
                e.Item.Cells[1].Text = "<a href=# style='text-decoration:underline'>" + e.Item.Cells[1].Text + "</a>";


                DataGridUtils.SetDataGridSelectedRowStyle(e);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.LoadData();
        }
    }
}